package cn.com.zzz.taskpk.base;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.bbs.ba.base.BaseDto;
import cn.bbs.ba.base.UserInfo;
import cn.bbs.ba.error.BAErrorMessage;
import cn.bbs.ba.factory.BABeanFactory;
import cn.bbs.ba.html.ZPaiInit;
import cn.bbs.ba.mi.BAMima;
import cn.bbs.ba.socket.BusCodeToBusBase;
import cn.bbs.ba.util.CommonUntil;
import cn.com.zzz.pk.dto.PkPepole;
import cn.com.zzz.pk.dto.PkTable;
import cn.com.zzz.services.UserServices;
import cn.com.zzz.taskpk.bussness.RoomBussness;
import cn.com.zzz.util.Constant;
import cn.com.zzz.util.DataFormatUtil;

/**
 * 扑克工程交易选择实现类
 * @author SL400
 */
public class PKBusCodeToBus extends BusCodeToBusBase{
	
    /** LOG输出 */
    protected static Log _log = LogFactory.getLog(PKBusCodeToBus.class);
	@Override
	public String doSelectBussness(String bussCode,
			BaseDto paramList) {
		BaseDto paraMap = null;
		try {
			paraMap = (BaseDto)paramList.get("ROOT");
		}
		catch (Exception ex) {
			String strError = "报文解析失败" + ex.toString();
			PKBusCodeToBus._log.error(strError);
			return strError;
		}
		
		try {
			int iBussCode = CommonUntil.objToint(bussCode);
			// 只要不是登陆
			if(BussnessCode.BUS_CODE00001 != iBussCode) {
				String logonUserID = CommonUntil.objToString(paraMap.getValue("logonUserID"));
				BaseDto retDto = new BaseDto();
				if (!logonUserID.equals(ZPaiInit.getUserMap().get(paraMap.getValue("userNo")))) {
					retDto.setHostRetCode(Constant.MSG_10002);
					retDto.setHostMessage(BAErrorMessage.push(Constant.MSG_10002));
					return DataFormatUtil.formatRetData(retDto);
				}
			}
			switch(iBussCode) {
				/**
				 * 前端登陆
				 */
			case BussnessCode.BUS_CODE00001:
				return qddl(paraMap);
				/**
				 * 大厅信息
				 */
			case BussnessCode.BUS_CODE00002:
				return dtInfo(paraMap);
				/**
				 * 房间信息
				 */
			case BussnessCode.BUS_CODE00003:
				return BUS_CODE00003(paraMap);
				/**
				 * 申请空闲桌位
				 */
			case BussnessCode.BUS_CODE00004:
				return RoomBussness.inRomm(paraMap);
				/**
				 * 退出桌位
				 */
			case BussnessCode.BUS_CODE00005:
				return RoomBussness.outRomm(paraMap);
				/**
				 * 取桌子信息
				 */
			case BussnessCode.BUS_CODE00006:
				return BUS_CODE00006(paraMap);
				
				/**
				 * 开始游戏
				 */
			case BussnessCode.BUS_CODE00007:
				return RoomBussness.startGame(paraMap);
				
				/**
				 * 押注
				 */
			case BussnessCode.BUS_CODE00008:
				return RoomBussness.yazhu(paraMap);
				
				/**
				 * 全押/弃牌/让牌
				 */
			case BussnessCode.BUS_CODE00009:
				return RoomBussness.caoZuo(paraMap);
				
				/**
				 * 没有用
				 */
			case BussnessCode.BUS_CODE00010:
				return RoomBussness.caoZuo(paraMap);
				
				/**
				 * 查看当前人剩余钱数
				 */
			case BussnessCode.BUS_CODE00011:
				return RoomBussness.userNowMoney(paraMap);
			}
			
		}
		catch (Exception ex) {
			String strError = ex.getMessage();
			PKBusCodeToBus._log.error(strError);
			return strError;
		}

		return "{\"root\":{hostRetCode:9999,hostMessage:\"没有对应的交易码\"}}";
	}
	
	/**
	 * 前端登陆
	 */
	private String qddl(BaseDto paraMap) {
		UserServices userServices = (UserServices)BABeanFactory.getBean("UserServices");
		UserInfo userinfo = new UserInfo();
		userinfo.setAlias(paraMap.getString("userName"));
		userinfo.setPASSPORT(BAMima.encry(userinfo.getAlias() + paraMap.getString("password")));
		
		return userServices.checkEX(userinfo);
	}
	
	/**
	 * 大厅信息
	 */
	private String dtInfo(BaseDto paraMap) {
		BaseDto retDto = new BaseDto();
		retDto.setHostRetCode(Constant.MSG_0000);
		retDto.setHostMessage(Constant.MSG_SUCCESS);
		retDto.setValue("LIST", ZPaiInit.getRoomList());
		retDto.setValue("size", ZPaiInit.getRoomList().size());
		return DataFormatUtil.formatRetData(retDto);
	}
	
	/**
	 * 房间信息
	 */
	private String BUS_CODE00003(BaseDto paraMap) {
		BaseDto retDto = new BaseDto();
		retDto.setHostRetCode(Constant.MSG_0000);
		retDto.setHostMessage(Constant.MSG_SUCCESS);
		
		retDto.setValue("LIST", ZPaiInit.getTableList(paraMap.getString("ROOMID")));
		return DataFormatUtil.formatRetData(retDto);
	}
	
	/**
	 * 桌子信息
	 */
	private String BUS_CODE00006(BaseDto paraMap) {
		PkTable table = ZPaiInit.getTable().get(paraMap.getString("tableId"));
		PkTable tableA = null;
		if (table != null) {
			tableA = (PkTable)table.deepCopy();
			tableA.setHostRetCode(Constant.MSG_0000);
			tableA.setHostMessage(Constant.MSG_SUCCESS);
			// 打牌中，把别人的牌给屏蔽掉
			if (tableA.getStt().equals("1")) {
				List<PkPepole> peopleList = (List<PkPepole>)tableA.getValue("people");
    			for(int i = 0; i < peopleList.size(); i++) {
    				PkPepole people = peopleList.get(i);
    				// 如果不是本人
    				if (!paraMap.getValue("userNo").equals(people.getUserNo())){
    					// 生产两张牌
    					people.setNowPK("*,*");
    				}
    			}
			}
		}
		else {
			tableA = new PkTable();
			tableA.setHostRetCode(Constant.MSG_9999);
			tableA.setHostMessage(Constant.MSG_ERROR);	
		}
		return DataFormatUtil.formatRetData(tableA);
	}
	
}
